package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.OrderDetailModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class OrderDetailManage {
    //根据订单ID添加订单记录
    public static void addOrderDetailByid(SQLiteDatabase db, String orderID, String bookID, String bookName, int num, String price) {
        ContentValues values = new ContentValues();
        values.put("orderID", orderID);
        values.put("bookID", bookID);
        values.put("bookname", bookName);
        values.put("num", num);
        values.put("price", price);
        db.insert("mall_orderdetail", null, values);
    }

    //根据订单号获取订单详情列表
    public static List<OrderDetailModel> getOrderDetailByid(SQLiteDatabase db, String orderID) {
        List<OrderDetailModel> orderDetailModels = new ArrayList<>();
        Cursor cursor = db.query("mall_orderdetail", new String[]{"orderID", "bookID", "bookName", "num", "price"}, "orderID=?"
                , new String[]{orderID}, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                OrderDetailModel orderDetailModel = new OrderDetailModel();
                orderDetailModel.setOrderID(orderID);
                orderDetailModel.setBookID(cursor.getString(1));
                orderDetailModel.setBookName(cursor.getString(2));
                orderDetailModel.setNum(cursor.getString(3));
                orderDetailModel.setPrice(cursor.getString(4));
                orderDetailModels.add(orderDetailModel);
            }
            return orderDetailModels;
        }
        cursor.close();
        return null;
    }
}
